package com.njegovanobradovic.pages.admin;

import java.util.List;

import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.hibernate.annotations.CommitAfter;
import org.apache.tapestry5.ioc.annotations.Inject;

import com.njegovanobradovic.entities.Page;
import com.njegovanobradovic.so.Delete;
import com.njegovanobradovic.so.Get;
import com.njegovanobradovic.so.GetAll;

public class AllPages {
	@Inject
	private GetAll<Page> getAllPages;
	@Property
	private Page page;
	
	public List<Page> getPages(){
		getAllPages.setSearchForType(Page.class);
		return getAllPages.execute();
	}
	
	
	@Inject
	private Delete<Page> deletePage;
	@CommitAfter
	public void onDelete(Long id) {
		try {
			deletePage.setId(id);
			deletePage.setSearchForType(Page.class);
			System.out.println(deletePage.execute());
		} catch (Exception e) {
			System.out.println("**********************Error on delete********************\n"+e);
		}
	}
	
	
	@Inject
	private Get<Page> getPage;
	@InjectPage
	private EditPage editPage;
	public Object onEdit(Long id) {
		try {
			getPage.setId(id);
			getPage.setSearchForType(Page.class);
			editPage.setPage(getPage.execute());
			return editPage;
		} catch (Exception e) {
			System.out.println("**********************Error on edit********************\n"+e);
			return null;
		}
	}
}
